package com.note.simpleadmin.module.shop.service;
import com.note.simpleadmin.dto.SpecDto;
import com.note.simpleadmin.entity.TbSpec;
import com.note.simpleadmin.entity.TbSpecOption;
import com.note.simpleadmin.exception.DataNotExistException;
import com.note.simpleadmin.module.shop.dao.SpecOptionRepository;
import com.note.simpleadmin.module.shop.dao.SpecRepository;
import com.note.simpleadmin.module.shop.vo.SpecVo;
import com.note.simpleadmin.utils.IdGenerator;
import com.note.simpleadmin.vo.PageBean;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/***
 *@ClaseName:BrandService
 *
 *@Decription:
 *
 *@Author: xubingbing
 *
 *@Date:2019/1/31 0031 20:59
 */
@Service
public class SpecService {
    @Autowired
    private SpecRepository specRepository;
    @Autowired
    private SpecOptionRepository specOptionRepository;
    public void delete(String id) throws DataNotExistException
    {
        TbSpec brand=specRepository.getOne(id);
        if(brand==null)
        {
            throw new DataNotExistException("数据不存在");
        }
        specRepository.delete(brand);
    }
    public void update(TbSpec tbSpec)throws DataNotExistException
    {
        TbSpec tbSpec1=specRepository.getOne(tbSpec.getId());
        if(tbSpec1==null)
        {
            throw new DataNotExistException("数据不存在");
        }
        specRepository.save(tbSpec);
    }
    @Transactional
    public void update(SpecDto dto)throws DataNotExistException
    {
        TbSpec tbSpec1=specRepository.getOne(dto.getId());
        if(tbSpec1==null)
        {
            throw new DataNotExistException("数据不存在");
        }
        tbSpec1.setName(dto.getName());
        tbSpec1.setSort(dto.getSort());
        specRepository.save(tbSpec1);
        //删除规格下面的所有选项
        specOptionRepository.deleteBySpecId(dto.getId());
        for(TbSpecOption option:dto.getSpecOptions())
        {
            option.setSpecId(dto.getId());
            option.setId(IdGenerator.id());
            specOptionRepository.save(option);
        }
    }
    public void save(TbSpec tbSpec)
    {
        tbSpec.setId(IdGenerator.id());
        specRepository.save(tbSpec);
    }
    @Transactional
    public void save(SpecDto dto)
    {
        TbSpec spec=new TbSpec();
        spec.setId(IdGenerator.id());
        spec.setName(dto.getName());
        spec.setSort(dto.getSort());
        for(TbSpecOption option:dto.getSpecOptions())
        {
            option.setSpecId(spec.getId());
            option.setId(IdGenerator.id());
            specOptionRepository.save(option);
        }
        specRepository.save(spec);
    }
    public List<SpecVo> findAll()
    {
        List<SpecVo> specVos=new ArrayList<SpecVo>();
        Sort sort = new Sort(Sort.Direction.DESC, "sort");
        List<TbSpec> specList=specRepository.findAll(sort);
        for(TbSpec spec:specList)
        {
            SpecVo specVo=new SpecVo();
            specVo.setId(spec.getId());
            specVo.setName(spec.getName());
            specVo.setSort(spec.getSort());
            specVo.setSpecOptions(specOptionRepository.findBySpecId(spec.getId()));
            specVos.add(specVo);
        }
        return specVos;
    }
    public PageBean findByPage(String name,Integer pageNumber)
    {
        if (pageNumber == null) {
            pageNumber = 0;
        }
        else
        {
            pageNumber=pageNumber-1;
        }
        Sort sort = new Sort(Sort.Direction.DESC, "sort");
        PageRequest pageRequest = new PageRequest(pageNumber, 20, sort);
        Specification<TbSpec> specification=new Specification<TbSpec>() {
            private static final long serialVersionUID = 1L;
            @Override
            public Predicate toPredicate(Root<TbSpec> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();
                if (StringUtils.isNotBlank(name)) {
                    predicates.add(criteriaBuilder.like(root.get("name").as(String.class),name + "%"));
                }
                return  criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        Page<TbSpec> page = specRepository.findAll(specification,pageRequest);
        List<TbSpec> classifies=page.getContent();
        PageBean<TbSpec> pageBean=new PageBean(page.getTotalPages(),page.getTotalElements(),page.isFirst(),page.isEmpty(),page.getNumber(), classifies);
        return pageBean;
    }
    public SpecVo getOne(String id)
    {
        SpecVo specVo=new SpecVo();
        TbSpec tbSpec=specRepository.getOne(id);
        specVo.setId(tbSpec.getId());
        specVo.setName(tbSpec.getName());
        specVo.setSort(tbSpec.getSort());
        List<TbSpecOption> options=specOptionRepository.findBySpecId(id);
        specVo.setSpecOptions(options);
        return specVo;
    }
}
